// pages/Home/content/content.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    replydis: 'none',//回复是否显示
    head_info: '',//优选信息
    advert: '',//广告信息
    zanhead: 'wei',//本条消息是否赞过
    iscollect: 'wei',//本条消息是否赞过
    hid: '',//本条信息id
    inputValue: '',//评论输入框内容
    comment_info: '',//评论信息
    iszanhead: 2,//2可以操作本条信息点赞，3不可以，防止重复点击
    isallzan: 2,//2可以操作评论信息点赞，3不可以，防止重复点击
    iscol: 2,//2可以操作收藏，3不可以，防止重复点击
    imgnum: 1,//banner第几张
    isChecked: false,
  },
  //banner变化
  swiperChange: function (e) {
    var imgnum = e.detail.current + 1;
    this.setData({
      imgnum: imgnum,
    });
  },

  serviceSelection:function(){
    this.setData({
      isChecked: true
    })  
  },



  //跳转到图片页
  toImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var typ = 'tou';
    var data = {
      id: id,
      typ: typ,
    }
    ikz.nav('../images/images', 'n', data);
  },
  //获取评论输入框内容
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value,
    })
  },
  //发表评论
  makeComment: function (e) {
    var that = this;
    var com_content = that.data.inputValue;
    //判断评论不能为空
    if (com_content == '') {
      wx.showToast({
        title: '请填写评论',
        duration: 2000
      });
      return false;
    }
    var hid = that.data.hid;
    var data = {
      hid: hid,
      com_content: com_content,
    }
    var path = 'Index/makeComment';
    ikz.request(path, data, true, (res) => {
      console.log(res);

      if (res.data.status == 'success') {
        var old_comment_info = that.data.comment_info;
        old_comment_info.unshift(res.data.comment_info);
        wx.showToast({
          title: '发表成功！',
          duration: 2000
        });
        that.setData({
          comment_info: old_comment_info,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },
  //本条消息收藏
  collectHead: function (e) {
    var hid = this.data.hid;
    var that = this;
    that.setData({
      iscol: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectHead';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'yi',
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已收藏！',
          duration: 2000
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //本条消息取消收藏
  haveCollect: function (e) {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    };
    that.setData({
      iscol: 3,
    });
    var path = 'Index/collectCancle';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '取消收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'wei',
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //本条消息点赞
  goHeadLike: function (e) {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    };
    that.setData({
      iszanhead: 3,
    });
    var path = 'Index/goHeadLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        that.setData({
          head_info: res.data.head_info,
          zanhead: 'yi',
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        iszanhead: 2,
      });
    });
  },
  //本条消息取消点赞
  noHeadLike: function (e) {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    };
    that.setData({
      iszanhead: 3,
    });
    var path = 'Index/noHeadLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        that.setData({
          head_info: res.data.head_info,
          zanhead: 'wei',
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iszanhead: 2,
      });
    });
  },
  //评论点赞
  goComLike: function (e) {
    var comid = e.currentTarget.dataset.comid;
    var that = this;
    var data = {
      comid: comid,
    };
    that.setData({
      isallzan: 3,
    });
    var path = 'Index/goComLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var comment_info = that.data.comment_info;
        for (var i = 0; i < comment_info.length; i++) {
          if (comment_info[i]['com_id'] == comid) {
            comment_info[i]['iszan'] = 'yi';
            comment_info[i]['com_like'] = parseInt(comment_info[i]['com_like']) + 1;
          };
        };
        that.setData({
          comment_info: comment_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //评论取消点赞
  noComLike: function (e) {
    var comid = e.currentTarget.dataset.comid;
    var that = this;
    var data = {
      comid: comid,
    };
    that.setData({
      isallzan: 3,
    });
    var path = 'Index/noComLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var comment_info = that.data.comment_info;
        for (var i = 0; i < comment_info.length; i++) {
          if (comment_info[i]['com_id'] == comid) {
            comment_info[i]['iszan'] = 'wei';
            comment_info[i]['com_like'] = parseInt(comment_info[i]['com_like']) - 1;
          };
        };
        that.setData({
          comment_info: comment_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //删除评论
  delCom: function (e) {
    var comid = e.currentTarget.dataset.comid;
    var hid = this.data.hid;
    var that = this;
    var data = {
      comid: comid,
      hid: hid,
    };
    wx.showModal({
      title: '提示',
      content: '您确认删除评论吗？',
      success: function (res) {
        if (res.confirm) {
          var path = 'Index/delCom';
          ikz.request(path, data, true, (res) => {
            console.log(res);
            if (res.data.status == 'success') {
              wx.showToast({
                title: '删除评论成功！',
                duration: 2000
              });
              var com_info = that.data.comment_info;
              var k = -1;
              for (var i = 0; i < com_info.length; i++) {
                if (comid == com_info[i].com_id) {
                  k = i;
                };
              };
              if (k > -1) {
                com_info.splice(k, 1);
              };
              that.setData({
                comment_info: com_info,
              })
            } else if (res.data.status == 'nochom') {
              wx.showModal({
                title: '提示',
                content: '不是本人评论，不能删！',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '服务器繁忙，请稍后重试！',
                showCancel: false
              })
            }

          });
        } else if (res.cancel) {
          return false;
        }
      }
    })

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var hid = options.hid;
    ikz.over(this);
    this.setData({
      hid: hid,
    });

  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  //时间转换
  diaplayTime(data) {
    var str = data;
    //将字符串转换成时间格式
    var timePublish = new Date(str);
    //现在时间
    var timeNow = new Date();
    //当天0点时间
    var timeStart = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var diffValue = timeNow - timePublish;
    var diffHour = diffValue / hour;
    var diffMinute = diffValue / minute;
    
    if (diffValue < 0) {
      wx.showToast({
        title: '时间错误！',
        duration: 2000
      });
      return false;
    }else{
      //判断是今天
      if (timePublish >= timeStart){
        if (diffHour > 1) {
          var result = parseInt(diffHour) + "小时前";
        }else if (diffMinute > 1) {
          var result = parseInt(diffMinute) + "分钟前";
        }else {
          var result = "刚刚发表";
        }
      } else {//判断不是今天
        var result = str;
      }
    }
    return result;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    }else{
      var islogin = true;
    }
    var path = 'Index/content';
    ikz.request(path, data, islogin, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var head_info = res.data.head_info;
        if (head_info.head_red_ismature == 2 && head_info.head_red_id > 0) {
          var noticedis = 'block';
        } else {
          var noticedis = 'none';
        }
        head_info.head_addtime = that.diaplayTime(head_info.head_addtime);
        that.setData({
          head_info: res.data.head_info,
          advert_info: res.data.advert_info,
          noticedis: noticedis,
          hid: hid,
          iscollect: res.data.iscollect,
          comment_info: res.data.comment_info,
          iszanhead: 2,
          isallzan: 2,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(111)

    
  },
   getRandomColor() {
    let rgb = []
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16)
      color = color.length == 1 ? '0' + color : color
      rgb.push(color)
    }
    return '#' + rgb.join('')
  },
  inputValue: '',
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: this.getRandomColor()
    })
  },
})